package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyCondition;
import cn.wolfcode.wolf2w.business.mapper.StrategyConditionMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyConditionQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyConditionService;
import cn.wolfcode.wolf2w.common.core.constant.ScheduleConstants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 攻略条件Service业务层处理
 *
 * @author 沙琪玛
 * @date 2024-11-01
 */
@Service
@Transactional
public class StrategyConditionServiceImpl extends ServiceImpl<StrategyConditionMapper, StrategyCondition> implements IStrategyConditionService {

    @Autowired
    StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCondition> queryPage(StrategyConditionQuery qo) {
        IPage<StrategyCondition> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCondition> queryCondition(Long type) {
        return lambdaQuery().eq(StrategyCondition::getType,type)
                .inSql(StrategyCondition::getStatisTime,"select max(statis_time) from ta_strategy_condition")
                .list();

    }

    @Override
    public void createCondition() {


        QueryWrapper<Strategy> qw = Wrappers.<Strategy>query()
                .eq("isabroad", 1)
                .groupBy("dest_id, dest_name")
                .select("dest_id refid, dest_name name, COUNT(*) count, 1 type");
        List<Map<String, Object>> abroadList = strategyMapper.selectMaps(qw);

        qw.clear();
        qw = Wrappers.<Strategy>query()
                .eq("isabroad", 0)
                .groupBy("dest_id, dest_name")
                .select("dest_id refid, dest_name name, COUNT(*) count, 2 type");
        List<Map<String, Object>> chinaList = strategyMapper.selectMaps(qw);

        qw.clear();
        qw = Wrappers.<Strategy>query()
                .groupBy("theme_id, theme_name")
                .select("theme_id refid, theme_name name, COUNT(*) count, 3 type");
        List<Map<String, Object>> themeList = strategyMapper.selectMaps(qw);

        List<Map<String, Object>> list = new ArrayList<>();
        list.addAll(abroadList);
        list.addAll(chinaList);
        list.addAll(themeList);

        Date now = new Date();

        for (Map<String, Object> map : list) {
            Long refid = Long.valueOf(map.get("refid").toString());
            String name = (String) map.get("name");
            Long count = Long.valueOf(map.get("count").toString());
            Long type = Long.valueOf(map.get("type").toString());

            StrategyCondition condition = new StrategyCondition();
            condition.setRefid(refid);
            condition.setName(name);
            condition.setCount(count);
            condition.setType(type);
            condition.setStatisTime(now);

            baseMapper.insert(condition);
        }


        System.out.println("攻略导航数据生成！");
    }
}
/*
*  QueryWrapper<Strategy> qw = Wrappers.<Strategy>query()
                .eq("isabroad", 1)
                .groupBy("dest_id, dest_name")
                .select("dest_id refid, dest_name name, COUNT(*) count");
        List<Map<String, Object>> abroadList = strategyMapper.selectMaps(qw);


        qw.clear();
        qw = Wrappers.<Strategy>query()
                .eq("isabroad", 0)
                .groupBy("dest_id, dest_name")
                .select("dest_id refid, dest_name name, COUNT(*) count");
        List<Map<String, Object>> chinaList = strategyMapper.selectMaps(qw);

        qw.clear();
        qw = Wrappers.<Strategy>query()
                .groupBy("theme_id, theme_name")
                .select("theme_id refid, theme_name name, COUNT(*) count");
        List<Map<String, Object>> themeList = strategyMapper.selectMaps(qw);*/